package com.example.hm_dtfkz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;

import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.config.LoginConfig;
import com.example.hm_dtfkz.databinding.DialogRedBubbleBinding;
import com.example.hm_dtfkz.util.ActivityClickUtil;
import com.example.hm_dtfkz.util.AnimUtil;
import com.example.hm_dtfkz.util.AppUtils;
import com.example.hm_dtfkz.util.Command;
import com.example.hm_dtfkz.util.DisplayUtil;
import com.dq.ad.utils.ToastUtil;
import com.example.hm_dtfkz.util.Mmkv;
import com.example.hm_dtfkz.util.sound.MediaBackManager;
import com.kc.openset.ad.listener.OSETRewardListener;
import com.kc.openset.ad.reward.OSETRewardVideo;

import java.util.Random;

public class DialogRedBubble extends Dialog implements View.OnClickListener {
    public static int TYPE_BUBBLE = 0;// 气泡红包
    public static int TYPE_SURPRISE = 1;// 惊喜红包
    public static int TYPE_PASS = 2;// 过关红包

    private DialogRedBubbleBinding bubbleBinding;
    private OnClickLister clickLister;
    private final Activity activity;
    private boolean isReward = true;
    private int dialogType = TYPE_BUBBLE;

    public DialogRedBubble(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bubbleBinding = DialogRedBubbleBinding.inflate(activity.getLayoutInflater());
        setContentView(bubbleBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        String title = "气泡红包";
        String tips = "观看激励视频越多\n气泡红包越大";
        if (dialogType == TYPE_SURPRISE) {
            title = "惊喜红包";
            tips = "每日限量赠送";
        } else if (dialogType == TYPE_PASS) {
            title = "过关红包";
            tips = "观看视频进行互动\n可翻倍获取红包";
        }
        bubbleBinding.tvTitle.setText(title);
        bubbleBinding.tvTips.setText(tips);

        AnimUtil.receiveAnimM(activity, bubbleBinding.ivGet);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                bubbleBinding.ivClose.setVisibility(View.VISIBLE);
            }
        }, 3000);

        AdManager.getInstance().showNativeAD(activity, bubbleBinding.nativeContainer, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                bubbleBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                bubbleBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {

            }

            @Override
            public void onClick() {

            }

            @Override
            public void onShow(View view) {

            }
        });

        AnimUtil.rotateAnim(bubbleBinding.ivCenterBg);
    }

    private void initLister() {
        bubbleBinding.ivCenter.setOnClickListener(this);
        bubbleBinding.ivClose.setOnClickListener(this);
    }

    public DialogRedBubble setType(int type) {
        this.dialogType = type;
        return this;
    }

    public DialogRedBubble setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.iv_center) {
            if (dialogType == TYPE_PASS) {
                if (clickLister != null) {
                    clickLister.onReceive(0);
                }
            } else {
                watchVideo();
            }
        } else if (v.getId() == R.id.iv_close) {
            if (dialogType == TYPE_PASS) {
                if (clickLister != null) {
                    clickLister.onClosed();
                }
            }
        }
    }

    private void watchVideo() {
        if (AppConfig.TD_BANNED == 1) {
            ToastUtil.showShortToast(activity, AppConfig.TD_BANNED_MSG);
            return;
        }

        isReward = false;
        if (AppConfig.hasNoAd()) {
            AppConfig.showNoAdToast(activity);
        } else {
            if (AppConfig.getBackMusic()) {
                MediaBackManager.getInstance(activity).pause();
            }

            OSETRewardVideo.getInstance()
                    .setContext(activity)
                    .setUserId(LoginConfig.getUserOpenId())
//                    .setPosId("4F6B4E8B71DC409875FF539214E0A81D")
                    .setPosId(AppUtils.getADID(3))
                    .showAd(activity, new OSETRewardListener() {
                        @Override
                        public void onClick() {

                        }

                        @Override
                        public void onClose(String s) {
//                            ToastUtil.showShortToast(activity, "未完整观看视频，领取失败");
                            MediaBackManager.getInstance(activity).start();
                        }


                        @Override
                        public void onReward(String s, int i) {
//                            ToastUtil.showShortToast(activity,i+"");
                            if (Mmkv.contains("cash_cow")) {
                                AppConfig.setCashCow(i+AppConfig.getCashCow());
                            }
                        }

                        @Override
                        public void onServiceResponse(int i) {

                        }

                        @Override
                        public void onShow(String s) {

                        }

                        @Override
                        public void onVideoEnd(String s) {
                            if (AppConfig.getBackMusic()) {
                                MediaBackManager.getInstance(activity).start();
                            }
                            if (isReward) {
                                if (dialogType == TYPE_SURPRISE) {
//                            UMManager.onEvent(UMManager.Event.Receive_SurpriseHongbao);
                                } else {
//                            UMManager.onEvent(UMManager.Event.Receive_BubbleHongbao);
                                }
                            } else {
//                                ToastUtil.showShortToast(activity, "未完整观看视频，领取失败");
                            }
                        }

                        @Override
                        public void onVideoStart() {
                            MediaBackManager.getInstance(activity)
                                    .pause();
                        }

                        @Override
                        public void onError(String s, String s1) {

                        }
                    });

        }
    }

    private void addCoin(String orderId) {
        int coin = new Random().nextInt(40) + 200;
//        HXCurrencySDK.getInstance().qaWatchVideo(2, coin, orderId, new HttpUtil.HttpUtilCallback() {
//            @Override
//            public void onSuccess(int i, String response) {
//                DQLog.e("看视频补充金币成功：" + response);
//                if (activity.isFinishing()) {
//                    return;
//                }
//                QAWatchVideoResp qaWatchVideoResp = JsonUtil.parseObject(response, QAWatchVideoResp.class);
//                if (qaWatchVideoResp != null) {
//                    if (qaWatchVideoResp.getErrorCode() == 0) {
//                        if (activity.isFinishing()) {
//                            return;
//                        }
//                        double total = qaWatchVideoResp.getAa();
//                        double bat = qaWatchVideoResp.getBat();
//                        double ra = qaWatchVideoResp.getRa();
//                        LoginConfig.setUserCash(LoginConfig.getUserCash() + ra);
//
//                        new DialogAddCoinResult(activity).setMoney(total).setClickLister(new DialogAddCoinResult.OnClickLister() {
//                            @Override
//                            public void onReceive() {
//                                if (clickLister != null) {
//                                    clickLister.onReceive(bat);
//                                }
//                            }
//                        }).show();
//                    } else {
//                        String errorMessage = qaWatchVideoResp.getErrorMessage();
//                        if (errorMessage.isEmpty()) {
//                            errorMessage = "错误码=" + qaWatchVideoResp.getErrorCode();
//                        }
//                        ToastUtil.showShortToast(activity, "气泡/惊喜/过关红包 看视频补充红包券失败：" + errorMessage);
//                    }
//                } else {
//                    ToastUtil.showShortToast(activity, "气泡/惊喜/过关红包 看视频补充红包券失败");
//                }
//            }
//
//            @Override
//            public void onFailed(int i, Throwable throwable) {
//                DQLog.e("气泡/惊喜/过关红包 看视频补充红包券失败：" + throwable.getMessage());
//                ToastUtil.showShortToast(activity, "气泡/惊喜/过关红包 看视频补充红包券失败：" + throwable.getMessage());
//            }
//        });
    }

    public interface OnClickLister {
        void onReceive(double bat);

        void onClosed();
    }
}